import openpyxl
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.utils import formataddr
# 批量发送邮件
workbook = openpyxl.load_workbook('mingdan.xlsx')
sheet = workbook.active
max_row = sheet.max_row
is_frist = True
for row in sheet.iter_rows(values_only=True):  # 读取行
    if is_frist:
        is_frist = False
    else:
        name = row[0]
        old_money = row[1]
        new_money = row[3]
        # 发件人邮箱账号
        sender = '957869384@qq.com'
        # 收件人邮箱账号
        receivers = ['13647694061@163.com']
        # 邮件内容
        message = MIMEText(
            f'<h1>通知<h1>你好：{name},由于<b>疫情原因</b>，公司调整薪资，由原来的{old_money}调整为<span style="color:red">{new_money}</span>。',
            'html',
            'utf-8')
        # 设置发件人信息
        message['From'] = formataddr(("重庆有缘科技", sender))
        # 设置收件人信息
        message['To'] = formataddr((f"{name}", ','.join(receivers)))
        # 设置邮件主题
        message['Subject'] = "薪资调整"
        try:
            # 连接SMTP服务器
            # smtp_obj = smtplib.SMTP('smtp.qq.com', 587)
            smtp_obj = smtplib.SMTP_SSL("smtp.qq.com", 465)
            # 开启安全传输
            # smtp_obj.starttls()
            # 登录发件人邮箱
            smtp_obj.login(sender, 'jjvxrnfzdlhrbcdc')
            # 发送邮件
            smtp_obj.sendmail(sender, receivers, message.as_string())
            print(f"给{name}的邮件发送成功！")
        except smtplib.SMTPException as e:
            print(f"给{name}的邮件发送失败: {e}")
        finally:
            # 关闭连接
            smtp_obj.quit()
